#include "managerservice_impl.h"
#include "manager.h"
#include "managerdao_file_impl.h"
#include <iostream>
#include <vector>
//#include "emis.h"
#include "getch.h"

using namespace std;
using namespace MH;


static vector <Manager> managers;

ManagerServiceImpl::ManagerServiceImpl(void)
{
	dao = new ManagerDaoFileImpl;
}
ManagerServiceImpl::~ManagerServiceImpl(void)
{
	delete dao;
}

void ManagerServiceImpl::addManager(void)
{
	cout << "输入要添加的id:" << endl;
	int _id=0;
	cin >> _id;
	cout << "输入要添加的姓名:" << endl;
	char _name[20]={};
	cin >> _name;
	cout << "输入要添加的密码:" << endl;
	char _password[20]={};
	cin >> _password;
	managers.clear();
	dao->load();
	Manager* me = new Manager(_id,_name,_password);
	managers.push_back(*me);
	dao->save();
	cout << "添加成功" << endl;
	getch();
	sleep(2);
}

void ManagerServiceImpl::deleteManager(void)
{
	cout << "输入要删除的id" << endl;
	int id = 0 ;
	cin >> id;
	managers.clear();
	dao->load();
	for(unsigned int i=0;i<managers.size();i++)
	{
		if(managers[i].id == id)
		{
			managers.erase(managers.begin()+i);
			cout << "删除成功" << endl;
			
			dao->save();
			getch();
			sleep(2);
			return;
		}
	}
		cout << "没有此人，删除失败!" << endl;
		getch();
		sleep(2);
}

void ManagerServiceImpl::listManager(void)
{
//	managers.clear();
//	dao->load();
	cout << "id" << " " << "姓名" << " " << "密码" << endl;
	for(unsigned int i=0; i<managers.size(); i++)
	{
		cout<<managers[i].id<<" "<<managers[i].name<<" "<<managers[i].password<<endl;;
	}
//	dao->save();
	sleep(2);
	getch();
}
